package com.wzs.easytobuy.service.impl;

import com.wzs.easytobuy.mapper.*;
import com.wzs.easytobuy.model.*;
import com.wzs.easytobuy.service.IProductService;
import com.wzs.easytobuy.utils.DateTimeUtil;
import com.wzs.easytobuy.utils.PageModel;
import com.wzs.easytobuy.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author miaomiao
 * @version 1.0.0
 * @Description 处理商品分类业务的接口实现类
 * @createTime 2021-06-24- 16:08:00
 */

@Service
public class IProductServiceImpl implements IProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductTypeMapper productTypeMapper;
    @Resource
    private FormatMapper formatMapper;
    @Resource
    private ColorMapper colorMapper;
    @Resource
    private SizeMapper sizeMapper;
    @Resource
    private HistoryMapper historyMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private LikeMapper likeMapper;

    @Override
    public Map<String, List<Map<String, List<ProductType>>>> getAll() {

        Map<String, List<Map<String, List<ProductType>>>> oneMap = new HashMap<>();
        List<ProductType> oneList = productTypeMapper.getTypeListById(0);
        for (ProductType oneType : oneList) {
            List<ProductType> twoList = productTypeMapper.getTypeListByIp(1, oneType.getName());
            List<Map<String, List<ProductType>>> TwoList = new ArrayList<>();
            Map<String, List<ProductType>> twoMap = new HashMap<>();
            for (ProductType twoType : twoList) {
                List<ProductType> threeList = productTypeMapper.getTypeListByIp(2, twoType.getName());
                twoMap.put(twoType.getName(), threeList);
            }
            TwoList.add(twoMap);
            oneMap.put(oneType.getName(), TwoList);
        }

        return oneMap;

    }

    @Override
    public ProductType selectByPrimaryKey(String id) {
        return productTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public ProductType selectByName(String productTypeName) {
        return productTypeMapper.selectByName(productTypeName);
    }

    @Override
    public List<ProductType> selectByPcAndName(String proColumn, String name) {
        return productTypeMapper.selectByPcAndName(proColumn, name);
    }

    @Override
    public PageModel<Product> productPageList(String productTypeId, PageModel<Product> pageModel, String productTypeName) {

        int totalRows = productMapper.selectAllCount(productTypeId, productTypeName);
        System.out.println(totalRows);
        pageModel.setTotalRows(totalRows);
        if (totalRows % pageModel.getPageSize() == 0) {
            pageModel.setPageTotalNum(totalRows / pageModel.getPageSize());
        } else {
            pageModel.setPageTotalNum(totalRows / pageModel.getPageSize() + 1);
        }

        List<Product> productList = productMapper.productPageList(productTypeId, pageModel.getSkipCount(), pageModel.getPageSize(), productTypeName);

        pageModel.setDataList(productList);

        return pageModel;
    }

    @Override
    public Product selectById(String id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public Format selectFormatById(String productId, Integer price, Integer sales) {
        return formatMapper.selectByIdAndPs(productId, price, sales);
    }

    @Override
    public List<Color> selectColorByPid(String productId) {
        return colorMapper.selectByPid(productId);
    }

    @Override
    public List<Size> selectSizeByPid(String productId) {
        return sizeMapper.selectByPid(productId);
    }

    @Override
    public void addHistory(String fId, String uId) {

        History history = new History();
        String id = UUIDUtil.getUUID();
        String browerTime = DateTimeUtil.getSysTime();

        history.setId(id);
        history.setFormatId(fId);
        history.setBrowerTime(browerTime);
        history.setUserId(uId);

        historyMapper.insert(history);

    }

    @Override
    public List<Map<String, Object>> selectHistoryByUid(String userId) {
        return historyMapper.selectAllByUid(userId);
    }

    @Override
    public PageModel<Map<String, Object>> orderPageList(String userId, PageModel<Map<String, Object>> pageModel) {

        int totalRows = orderMapper.selectAllCount(userId);
        pageModel.setTotalRows(totalRows);
        if (totalRows % pageModel.getPageSize() == 0) {
            pageModel.setPageTotalNum(totalRows / pageModel.getPageSize());
        } else {
            pageModel.setPageTotalNum(totalRows / pageModel.getPageSize() + 1);
        }

        List<Map<String, Object>> orderList = orderMapper.orderPageList(userId, pageModel.getSkipCount(), pageModel.getPageSize());

        pageModel.setDataList(orderList);

        return pageModel;
    }

    @Override
    public PageModel<Map<String, Object>> likePageList(String userId, PageModel<Map<String, Object>> pageModel) {

        int totalRows = likeMapper.selectAllCount(userId);
        pageModel.setTotalRows(totalRows);
        if (totalRows % pageModel.getPageSize() == 0) {
            pageModel.setPageTotalNum(totalRows / pageModel.getPageSize());
        } else {
            pageModel.setPageTotalNum(totalRows / pageModel.getPageSize() + 1);
        }

        List<Map<String, Object>> likeList = likeMapper.likePageList(userId, pageModel.getSkipCount(), pageModel.getPageSize());

        pageModel.setDataList(likeList);

        return pageModel;

    }

    @Override
    public boolean deleteLike(String id) {
        boolean flag = true;

        int count = likeMapper.deleteByPrimaryKey(id);

        if (count != 1) {
            flag = false;
        }

        return flag;

    }

    @Override
    public boolean addLike(String productId, String userId, String price, String sales) {

        boolean flag = true;

        Like like = new Like();
        String id = UUIDUtil.getUUID();
        Format format = formatMapper.selectByIdAndPs(productId, Integer.parseInt(price), Integer.parseInt(sales));

        Like l = likeMapper.selectLikeByFidAndUid(format.getId(), userId);

        //如果该商品没有被收藏
        if (l == null) {
            like.setId(id);
            like.setUserId(userId);
            like.setFormatId(format.getId());
            like.setLikeTime(DateTimeUtil.getSysTime());

            int count = likeMapper.insert(like);

            if (count != 1) {
                flag = false;
            }
        } else {
            throw new RuntimeException("该商品已被收藏");
        }
        return flag;

    }

    @Override
    public Like selectLikeByUidAndFid(String formatId, String userId) {
        return likeMapper.selectLikeByFidAndUid(formatId, userId);
    }

    @Override
    public Format selectByCidAndSid(String productId, String colorId, String sizeId) {

        return formatMapper.selectByCidAndSid(productId, colorId, sizeId);
    }

    @Override
    public Format selectByCidAndSid2(String productId, String colorId, String sizeId) {
        return formatMapper.selectByCidAndSid2(productId, colorId, sizeId);
    }

    @Override
    public boolean deleteHistory(String userId) {
        return historyMapper.deleteByUid(userId);
    }
}















